#!/bin/bash

current_path=$(cd $(dirname $0) && pwd)
test_name="SW_TEST_CMD"
test_time=$(date "+%Y%m%d%H%M%S")

echo -e "输入测试目标路数："
#键盘读取赋值给变量test_count
read test_count
#判断test_count的值是否为数字，条件否则直接退出
expr $test_count + 0 1>/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "${test_count} is not a number!"
exit 0
fi
#判断变量test_count的值是否大于0
if [ "$test_count" -gt 0 ]
then
echo "测试目标路数：${test_count}"
else
exit 0
fi

test_log="${current_path}/logs/${test_name}_${test_count}_${test_time}.log"


echo "测试方案：${test_name}"
echo "测试路数：${test_count}"
echo "测试时间：${test_time}"
echo "测试记录：${test_log} "
echo "测试方案：${test_name}" >> ${test_log}
echo "测试路数：${test_count}" >> ${test_log}
echo "测试时间：${test_time}" >> ${test_log}
echo "测试记录：${test_log} " >> ${test_log}


ffmepgBin="${current_path}/../ffmpeg-4.3.1/ffmpeg"
cd ${current_path}
pwd
./stop.sh
sleep 1
killall ffmpeg
sleep 1

inputScheam="rtsp"
inputHost="192.168.1.162"
inputPort="554"
inputUrl="${inputScheam}://${inputHost}:${inputPort}/live/test"

outputScheam="rtmp"
outputHost="192.168.1.162"
outputPort="1935"

echo "开始执行..." >> ${test_log}

echo "-----------------------------------------------------------"
echo "-----------------------------------------------------------" >> ${test_log}
for((i=0;i<$test_count;i++))
do
    
    outputUrl="${outputScheam}://${outputHost}:${outputPort}/live/ch${i}";
    cmd="${ffmepgBin} -threads 1 -rtsp_transport tcp -probesize 10240 -analyzeduration 1000000 -fpsprobesize 25 -i ${inputUrl} -vcodec copy -an -f flv ${outputUrl}"
    echo "${cmd}"
    echo "${cmd}" >> ${test_log}
    ${cmd} >/dev/null 2>&1 &
    # top -b -n 1 | head -n 5
    sleep 0.2
    echo "-----------------------------------------------------------"
    echo "-----------------------------------------------------------"  >> ${test_log}
done

echo "开始记录..."
echo "开始记录..." >> ${test_log}

echo "-----------------------------------------------------------"
echo "-----------------------------------------------------------"  >> ${test_log}
# 400*3=1200秒=20分钟
for((i=0;i<401;i++))
do
    current_time=$(date "+%Y%m%d%H%M%S")
    ((sec=${i}*3))

    echo "[current_time:${current_time}][sec：${sec}][test：${test_name}][stream：${test_count}]"
    echo "[current_time:${current_time}][sec：${sec}][test：${test_name}][stream：${test_count}]"  >> ${test_log}
    
    uptime

    # uptime
    echo -e "\tload: "`cat /proc/loadavg` >> ${test_log}

    # ffmpeg count
    echo -e "\tffmpeg count: "`pgrep ffmpeg | wc | awk '{print $1}'` >> ${test_log}

    # mem info
    echo -e "\tmem used: "`free -h | grep Mem: | awk '{print $3}'` >> ${test_log}

    # netstat CLOSE_WAIT 
    echo -e "\tCLOSE_WAIT cnt: "`netstat -anpt 2>/dev/null | grep ffmpeg | grep CLOSE_WAIT | wc | awk '{print $1}'` >> ${test_log}

    # network rate 
    echo -e "\teth0 rx rate: "`ifstat -bt -t 3 1 | tail -n1 | awk '{print $2}'` >> ${test_log}

    echo "-----------------------------------------------------------"
    echo "-----------------------------------------------------------" >> ${test_log}
done
